package com.qixianxian.servlet;

import com.qixianxian.pojo.User;
import com.qixianxian.service.UserService;
import com.qixianxian.service.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "Servlet1", value = "/ser1")
public class Servlet1 extends HttpServlet {

    private UserServiceImpl usi;
    public Servlet1() {
        usi = new UserServiceImpl();
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        System.out.println("servlet1 进入");
        String uname = req.getParameter("uname");
        String pwd = req.getParameter("pwd");
        User user = usi.Login(uname, pwd);
        if(user != null) {
            HttpSession session = req.getSession();
            session.setAttribute("user", user);
            resp.sendRedirect("ser2");
        }
        else {
            resp.getWriter().write("用户名或密码错误，请重新输入！");
        }
        System.out.println("servlet1退出");
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
